package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import com.qingxin.park.entity.ProvenanceImgs;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.MyUploadFile;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;


/**
 * 生产环节记录图片
 * @author Administrator
 *
 */
@Service
public class ProvenanceImgsService extends BaseService<ProvenanceImgs, String> {

	
	/**
	 * 获取指定生产环节记录的图片
	 * @param proid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ProvenanceImgs> listprovenanceImgs(String proid){
		List<ProvenanceImgs> list = listByHql("from ProvenanceImgs p where p.proid=? order by sort asc", proid);
		if(list == null){
			return new ArrayList<>();
		}
		return list;
	}
	
	
	/**
	 * 添加图片
	 * @param recordid
	 * @param uploadFileParam
	 * @param loginUser
	 * @return
	 */
	public SysError add(String proid, MyUploadFileParam uploadFileParam, LoginUser loginUser){
		
		/*ProRecord proRecord = new ProRecord();
		proRecord.setRecordid(proid);*/
		
		//删除
		if(uploadFileParam.getDeleteNum() > 0){
			String[] ids = uploadFileParam.getDeleteIds();
			for(String id : ids){
				ProvenanceImgs provenanceImgs = get(id);
				this.delete(provenanceImgs);
			}
		}
		
		//修改
		int modifyNum = uploadFileParam.getModifyNum();
		if(modifyNum > 0){
			for(int i=0; i<modifyNum; i++){
				MyUploadFile uploadFile = uploadFileParam.getModify(i);
				ProvenanceImgs img = getSafeEntity(uploadFile.getFileId());
				if(img == null){
					continue;
				}
				img.setImgDesc(uploadFile.getFileDesc());
				Updater<ProvenanceImgs> updater = new Updater<>(img, "imgDesc");
				this.update(updater);
			}
		}
		
		//添加
		int uploadNum = uploadFileParam.getUploadNum();
		if(uploadNum > 0){
			for(int i=0; i<uploadNum; i++){
				MyUploadFile uploadFile = uploadFileParam.getUpload(i);
				//从临时目录移动到保存目录
				UploadFile.moveTmpTo(uploadFile.getFileName(), UploadFile.DIR_PRO_IMGS);
				
				ProvenanceImgs provenanceImgs = new ProvenanceImgs(proid, uploadFile.getOrigName(),
						uploadFile.getFileName(), uploadFile.getFileDesc(), "", i, loginUser.getLogin_name(), new Date());
				this.save(provenanceImgs);
			}
		}
		
		return null;
	}
	
	
	
	
	
}
